package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Override
    public PageResult findPage(QueryPageBean pageBean) {
        //分页查询
            Integer currentPage = pageBean.getCurrentPage();
            Integer pageSize = pageBean.getPageSize();
            String queryString = pageBean.getQueryString();
            //使用分页助手插件实现分页查询
            PageHelper.startPage(currentPage,pageSize);
            Page<Role> page = roleDao.findByCondition(queryString);
            return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Role role, Integer[] menuIds, Integer[] permissionIds) {
        roleDao.add(role);
        Integer roleId = role.getId();
        this.setAssociation(roleId,menuIds,permissionIds);
    }

    @Override
    public void edit(Role role, Integer[] menuIds, Integer[] permissionIds) {
        roleDao.edit(role);
        Integer roleId = role.getId();
        this.setAssociation(roleId,menuIds,permissionIds);
    }

    @Override
    public Role findById(Integer id) {
        Role role = roleDao.findById(id);
        return role;
    }


    @Override
    public Integer[] findPermissionIdsByRoleID(Integer id) {
        Integer[] permissionIds = roleDao.findPermissionIdsByRoleID(id);
        return permissionIds;
    }

    @Override
    public Integer[] findMenuIdsByRoleID(Integer id) {
        Integer[] menuIds = roleDao.findMenuIdsByRoleID(id);
        return menuIds;
    }

    @Override
    public void deleteById(Integer id) {
        roleDao.deleteById(id);
    }



    public void setAssociation(Integer roleId, Integer[] menuIds, Integer[] permissionIds) {
        this.deleteAssociation(roleId);
        if (menuIds != null && menuIds.length > 0) {
            for (Integer menuId : menuIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("roleId",roleId);
                map.put("menuId",menuId);

                roleDao.setRoleAndMenu(map);
            }
        }
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("roleId",roleId);
                map.put("permissionId",permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }

    private void deleteAssociation(Integer roleId) {
        roleDao.deleteRole_MenuById(roleId);
        roleDao.deleteRole_PermissionById(roleId);
    }
}
